package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Region;
import cn.wolfcode.domain.StrategyTheme;
import cn.wolfcode.query.StrategyThemeQuery;
import cn.wolfcode.repository.RegionRepository;
import cn.wolfcode.repository.StrategyThemeRepository;
import cn.wolfcode.service.IRegionService;
import cn.wolfcode.service.IStrategyThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyThemeServiceImpl implements IStrategyThemeService {

    @Autowired
    private StrategyThemeRepository strategyThemeRepository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void insert(StrategyTheme strategyTheme) {
        strategyThemeRepository.save(strategyTheme);
    }

    @Override
    public void update(StrategyTheme strategyTheme) {
        strategyThemeRepository.save(strategyTheme);
    }

    @Override
    public void delete(String id) {
        strategyThemeRepository.deleteById(id);
    }

    @Override
    public StrategyTheme get(String id) {
        return strategyThemeRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyTheme> listAll() {
        return strategyThemeRepository.findAll();
    }

    @Override
    public Page query(StrategyThemeQuery qo) {
        //创建查询对象
        Query query = new Query();
        //查询总条数
        long count = template.count(query, StrategyTheme.class);
        if (count == 0){
            return Page.empty();
        }
        Pageable page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(page);
        List<StrategyTheme> list = template.find(query, StrategyTheme.class);
        PageImpl<StrategyTheme> regions = new PageImpl<>(list, page, count);
        return regions;
    }
}
